<template>
  <div class="app-container">
    <el-row type="flex" align="top" justify="space-between">
      <el-button type="primary" plain @click="add">新建试卷</el-button>
      <el-form inline :model="formInline">
        <el-form-item label="试卷名称">
          <el-input
            v-model="formInline.name"
            clearable
            placeholder="请输入试题内容"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        label="试卷名称"
        align="center"
        prop="name"
        show-overflow-tooltip
      />
      <el-table-column
        label="试题总数"
        align="center"
        prop="questionNum"
        width="120"
      />
      <el-table-column label="是否限时" align="center" width="120">
        <template slot-scope="{ row }">
          <span>{{ row.isTimeLimit === 2 ? "限时" : "不限时" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="120">
        <template slot-scope="{ row }">
          <el-tooltip
            :content="row.status == 2 ? '已发布' : '未发布'"
            placement="top"
          >
            <el-switch
              :disabled="row.status == 2"
              :value="row.status === 2"
              @change="publish(row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        label="创建人"
        align="center"
        prop="createBy"
        width="150"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="200"
      />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="{ row }">
          <el-button size="mini" type="primary" plain @click="edit(row.id)">
            {{ row.status == 2 ? "查看" : "编辑" }}
          </el-button>
          <el-button
            size="mini"
            type="danger"
            plain
            :disabled="row.status == 2"
            @click="remove(row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px" />
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="pageNum"
      :page-size="pageSize"
      :total="total"
      @size-change="onPageSizeChange"
      @current-change="onCurrentPageChange"
    />
  </div>
</template>

<script>
import {
  getExamPaperPage,
  publishExamPaper,
  deleteExamPaper,
} from "@/api2/examPaper";

export default {
  data() {
    return {
      formInline: {},
      tableData: [],
      listLoading: true,
      pageNum: 1,
      pageSize: 10,
      total: 0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      getExamPaperPage({
        ...this.formInline,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then((data) => {
        this.listLoading = false;
        this.tableData = data.data;
        this.total = data.total;
      });
    },
    onPageSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    onCurrentPageChange(val) {
      this.pageNum = val;
      this.getList();
    },
    search() {
      this.pageNum = 1;
      this.getList();
    },
    async publish(row) {
      let { status, id } = row;
      status = status == 2 ? 1 : 2;
      await publishExamPaper({ id, status });
      row.status = status;
      this.$message.success(status == 2 ? "发布成功" : "取消发布");
    },
    add() {
      this.$emit("change", {
        componentName: "examPaperDetail",
        id: "",
      });
    },
    edit(id) {
      this.$emit("change", {
        componentName: "examPaperDetail",
        id,
      });
    },
    remove(id) {
      this.$confirm("确定删除此项数据吗?", {
        type: "warning",
      })
        .then(async () => {
          await deleteExamPaper(id);
          this.getList();
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
